<?php

namespace App\Models\Dynamic;

use App\User;
use Illuminate\Database\Eloquent\Model;

class BloodMedication extends Model
{
    protected $table = "tn_blood_medication";

    public $timestamps = false;

    protected $fillable = [
        'remark', 'medicationtime', 'durationtime', 'userid', 'addtime', 'isdel'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->HasOne(User::class, 'id', 'userid');
    }


    /**
     * @param $userid
     * @param $startTime
     * @param $endTime
     * @param int $type
     * @return array
     */
    public function getInfoListByUser($userid, $startTime, $endTime, $type = 0)
    {
        if ($type == 1) {
            $usage = [1, 2];
        } else {
            $usage = [3, 4];
        }
        $list = $this->whereUserid($userid)
            ->whereBetween('medicationtime', [$startTime, $endTime])
            ->where('tn_blood_medication_info.isdel',0)
            ->where('tn_blood_medication.isdel',0)
            ->join('tn_blood_medication_info', 'tn_blood_medication.id', '=', 'tn_blood_medication_info.medicationid')
            ->whereIn('tn_blood_medication_info.usage', $usage)
            ->distinct()
            ->get()
            ->toArray();

        return $list;
    }

}
